import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AppEditionService} from './appEdition.service';
import {BaseComponent} from '../common/base.component';
import {AppEditionPage, AppEdition} from './appEdition';

declare var $: any;

@Component({
  selector: 'app-edition',
  templateUrl: './appEdition.component.html'
})
export class AppEditionComponent extends BaseComponent implements OnInit {
  appIdInfo: String = '';
  companyId: String = ''; // 改变公司名称
  idInfo: String = '';    // 根据id删除
  editorTitle: String = '';
  isAddEditor: boolean;
  curPage: Number = 1;
  appEditionPage: AppEditionPage;
  appEdition: AppEdition[];
  curAppEdition: AppEdition;
  companyInfo: any[];

  constructor(private appEditionService: AppEditionService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curAppEdition = {
      companySign: '',
      id: '',
      number: '',
      companyName: '',
      type: '',
      url: '',
      userName: '',
      isCompelUp: '',
      createTime: ''
    };
    this.searchAppEdition();
  }

  // 新增
  onAddAppEdition() {
    this.isAddEditor = true;
    this.editorTitle = '新增APP';
    this.curAppEdition = {
      companySign: '',
      id: '',
      number: '',
      companyName: '',
      type: '',
      url: '',
      userName: '',
      isCompelUp: '',
      createTime: ''
    };
    $('#data_editor').modal('show');
    this.getCompany();
  }

// 修改
  onEditAppEdition(value: AppEdition) {
    this.isAddEditor = false;
    this.editorTitle = '修改APP';
    this.curAppEdition = value;
    this.appIdInfo = this.curAppEdition.id;
    $('#data_editor').modal('show'); // 显示编辑对话框
    this.getCompany();
  }

// 删除
  onDeleteApp(value: AppEdition) {
    this.curAppEdition = value;
    this.idInfo = this.curAppEdition.id;
    $('#delete_confirm').modal('show'); // 显示编辑对话框
  }

// 新增/修改
  save() {
    if (this.isAddEditor) { // 新增数据
      this.curPage = 1;
      this.appEditionService.addAppEdition(this.curAppEdition, this.companyId)
        .then(result => {
          if (result) {
            this.alert.info('新增成功');
            this.queryList();
          } else {
            this.alert.error('新增失败');
          }
        });
    } else { // 修改数据
      this.appEditionService.updateAppinfo(this.curAppEdition, this.appIdInfo)
        .then(result => {
          if (result) {
            this.alert.info('修改成功');
            this.queryList();
          } else {
            this.alert.error('修改失败');
          }
        });
    }
    $('#data_editor').modal('hide');
  }

  // 删除
  delete() {
    this.appEditionService.removeUserinfo(this.idInfo)
      .then(result => result ? this.queryList() : alert('数据删除失败，请重试！'));
    $('#delete_confirm').modal('hide');
  }

  // 查询app
  searchAppEdition() {
    this.curPage = 1;
    this.queryList();
  }

  onPage(event) {
    this.curPage = event;
    this.queryList();
  }

  private queryList() {
    this.appEditionService.getAppEdition(this.curPage)
      .then(page => {
          this.appEditionPage = page;
          this.appEdition = page.appEdition.datas;
        }
      );
  }

  private getCompany() {
    this.appEditionService.getCompanyService()
      .then(res => {
          if (res[0]) {
            this.companyInfo = res[1];
          }
        }
      );
  }

  // 选择公司
  selAppCompany(id: string) {
    this.companyId = id;
  }
}
